<?php
/*
 * @o: ========================================================
 * @Created By: VsCode
 * @Author: Fu
 * @name: 管理员控制器
 * @LastEditTime: 2021-05-13 13:43:59
 * @Date: 2021-04-02 18:13:40
 * @u: ========================================================
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\Banner as BannerModel;
use app\admin\model\goods\Goods_category as GoodsCategoryModel;
use app\admin\model\goods\Product as ProductModel;
use app\admin\model\goods\Goods as GoodsModel;
use think\Db;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function banner(BannerModel $BannerModel)
    {
        $field = 'banner_image';
        $banner_list = $BannerModel->getList($field)->toArray();
        $this->success('成功',$banner_list);
    }


    public function  categorylist(GoodsCategoryModel $GoodsCategoryModel)
    {
        $field = 'id,title,image';
        $GoodsCategoryList = $GoodsCategoryModel->getList($field)->toArray();
        $this->success('成功',$GoodsCategoryList);
    }

    public function productlist(ProductModel $ProductModel)
    {
        $field = '*';
        $ProductModel = $ProductModel->getList($field);
        $this->success('成功',$ProductModel);
    }

    public function goods(GoodsModel $GoodsModel)
    {
        // $field = 'id,goods_image,goods_name,goods_rice';
        $GoodsModel = Db::name('goods')
                    ->alias('a')
                    ->join('fa_order_child oc','a.id = oc.goods_id','LEFT')
                    ->field(['a.id, a.goods_name, a.goods_image, a.goods_rice, sum(oc.goods_id) as count'])
                    ->group('a.id')
                    ->where('goodsswitch',1)
                    ->select();
        // print_r($GoodsModel);
        $GoodsModel = $this->shuffle_assoc($GoodsModel);
        $this->success('成功',$GoodsModel);
    }

    
    public function shuffle_assoc($list) {   
        if (!is_array($list)) return $list;   
        $keys = array_keys($list);  
        shuffle($keys);   
        $random = array();   
        foreach ($keys as $key) {
            $random[$key] = $list[$key];   
        }  
        return $random;
    }
}
